﻿using System;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ClientProsecution : BasePage<ClientProsecutionViewModel>, IPermittedPage
    {

        /// <summary>
        /// Gets the loan ID.
        /// </summary>
        private string LoanID
        {
            get { return Request.QueryString["LoanID"].ToString(); }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {
                if (!LoanID.IsNullOrEmpty())
                {
                    ICustomerLoan loan = this.ViewModel.GetLoan(LoanID.ToGuid());
                    if (loan != null)
                    {
                        txtLoanNumber.Text = loan.LoanNumber.ToString();
                        txtCustomerName.Text = loan.CustomerObj.CustomerName;
                        dateCaseDate.MinDate = loan.LoanCreatedDate;
                    }
                }
                else
                {
                    this.RedirectToPreviousPage();
                }
            }
        }



        /// <summary>
        /// Handles the Clicked event of the BtnProsecute control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnProsecute_Clicked(object sender, AjaxEventArgs e)
        {
            if (!LoanID.IsNullOrEmpty())
            {
                this.ViewModel.CreateLoanCase(LoanID.ToGuid(), long.Parse(txtCaseNumber.Text), txtNotes.Text, dateCaseDate.SelectedDate);
                this.RedirectToPreviousPage();
            }
        }

        public string PermissionName
        {
            get { return Permissions.ClientProsecution.ToString(); }
        }
    }
}
